/**
 * @(#)ICodeManage.java 07/23/2013
 *
 * Copyright (c) 2013 S9,Inc.All rights reserved.
 * Created by 2013-07-23 
 */
package cn.jdy.authority.admin;

import cn.jdy.authority.entity.SysRole;
import cn.jdy.authority.entity.User;
import cn.jdy.authority.service.SysRoleService;
import cn.jdy.authority.service.UserService;
import cn.jdy.framework.log.BaseLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统角色管理 该控制类提供了角色的增删改查功能
 *
 * @author: zhangyuyong
 * @date: 2013-7-23
 */
@Controller
@RequestMapping("/role")
public class SysRoleCtrl {
	@Autowired
	public SysRoleService sysRole;
	@Autowired
	public UserService userService;

	/**
	 * 初始系统角色列表
	 *
	 * @description: 将所有的角色信息取出来
	 */
	@RequestMapping("initRole")
	@ResponseBody
	public Map<String, Object> initRole() {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<SysRole> roleList = sysRole.findAll();
			map.put("msg", "success");
			map.put("roleList", roleList);
		} catch(Exception e) {
			map.put("msg", "error");
			BaseLog.e(this.getClass(), "initRoleTree初始化角色信息失败", e);
		}
		return map;
	}

	/**
	 * 获取角色基本信息
	 *
	 * @param roleId 角色标识
	 * @return ModelAndView /s9/role/roleAndUser_init.jsp
	 * @description: 根据角色id获得角色基本信息
	 * @author:zhangli
	 * @date:2013-7-24
	 */
	@RequestMapping("/initUserOfRole")
	public ModelAndView initUserOfRole(String roleId) {
		ModelAndView mv = new ModelAndView("/s9/role/roleAndUser_init.jsp");
		try {
			SysRole role = sysRole.findOne("id", roleId);
			mv.addObject("role", role);
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "initUserOfRole根据角色id获取角色基本信息失败", e);
		}
		return mv;
	}

	/**
	 * 添加角色初始化
	 *
	 * @return ModelAndView /s9/role/roleManage_add.jsp
	 * @description: 弹出添加角色的表单
	 * @author:zhangli
	 * @date:2013-7-25
	 */
	@RequestMapping("/forInitAddRole")
	public ModelAndView forInitAddRole() {
		return new ModelAndView("/s9/role/roleManage_add.jsp");
	}

	/**
	 * 添加或修改角色
	 */
	@RequestMapping("/saveRole")
	@ResponseBody
	public Map<String, Object> saveRole(SysRole role) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			String id = null;
			if(role.getId() == null || role.getId().isEmpty()) {
				role.setId(null);
				id = String.valueOf(sysRole.save(role));
			} else {
				sysRole.update(role);
				id = role.getId();
			}
			jsonMap.put("statusCode", 200);
			jsonMap.put("id", id);
		} catch(Exception e) {
			jsonMap.put("statusCode", 500);
			jsonMap.put("msg", "保存基本信息失败");
			BaseLog.e(this.getClass(), "save角色异常", e);
		}
		return jsonMap;
	}

	/**
	 * 修改角色初始化
	 *
	 * @param roleId
	 * @return ModelAndView /s9/role/roleManage_upd.jsp
	 * @description: 跳转至修改角色页面
	 * @author:zhangli
	 * @date:2013-7-25
	 */
	@RequestMapping("/forInitUpdRole")
	public ModelAndView forInitUpdRole(String roleId) {
		ModelAndView mv = new ModelAndView("/s9/role/roleManage_upd.jsp");
		try {
			SysRole role = sysRole.findOne("id", roleId);
			mv.addObject("role", role);
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "forInitUpdRole修改角色初始化失败", e);
		}
		return mv;
	}

	/**
	 * 删除角色信息
	 *
	 * @param id 角色标识
	 * @return Map 执行成功标志
	 * @description: 将数据库的该条角色信息删除
	 * @author:zhangli
	 * @date:2013-7-25
	 */
	@RequestMapping("delRole")
	@ResponseBody
	public Map<String, Object> delRole(String id) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			sysRole.delRole(id);
			jsonMap.put("statusCode", 200);
		} catch(Exception e) {
			jsonMap.put("statusCode", 500);
			jsonMap.put("msg", "删除角色信息失败！");
			BaseLog.e(this.getClass(), "delRole删除角色信息失败", e);
		}
		return jsonMap;
	}

	/**
	 * 初始化角色用户
	 *
	 * @return Map<String,Object> 用户总数和当前页用户列表
	 * @description: 根据选中的角色id查找该角色下所有的用户
	 * @author:zhangli
	 * @date:2013-7-29
	 */
	@RequestMapping("initUserRole")
	@ResponseBody
	public Map<String, Object> initUserRole(SysRole role) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			jsonMap.put("dataList", userService.findUserByRoleId(role));
			int total = sysRole.count(role);
			jsonMap.put("iTotalRecords", total);
			jsonMap.put("iTotalDisplayRecords", total);
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "initUserRole根据角色id初始属于这个角色的所有用户失败", e);
		}
		return jsonMap;
	}

	@RequestMapping("/findUsers")
	@ResponseBody
	public Map<String, Object> findUsers(String roleid, int iDisplayStart, int iDisplayLength) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			
			List<User> list = userService.findUsers(roleid, iDisplayStart, iDisplayLength);
			int total = userService.countUsers(roleid);
			jsonMap.put("dataList", list);
			jsonMap.put("iTotalRecords", total);
			jsonMap.put("iTotalDisplayRecords", total);
		} catch(Exception e) {
			jsonMap.put("statusCode", 500);
			BaseLog.e(this.getClass(), "查询会员异常", e);
		}
		return jsonMap;
	}

	/**
	 * 初始化用户列表
	 *
	 * @return ModelAndView /s9/role/roleAndUser_add.jsp
	 * @description: 将用户添加到角色初始化，查询所有用户
	 * @author:zhangli
	 * @date:2013-7-29
	 */
	@RequestMapping("forInitAddUser")
	public ModelAndView forInitAddUser(String roleId) {
		ModelAndView mv = new ModelAndView("/s9/role/roleAndUser_add.jsp");
		mv.addObject("roleId", roleId);
		return mv;
	}

	/**
	 * 添加用户与角色的关联数据
	 *
	 * @return map成功标志
	 * @description: 将选择的用户与角色信息关系数据存入关系表中
	 * @author:zhangli
	 * @date:2013-8-1
	 */
	@RequestMapping("addUserToRole")
	@ResponseBody
	public Map<String, Object> addUserToRole(String roleid, String userids) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			sysRole.addUserToRole(userids, roleid);
			jsonMap.put("statusCode", 200);
		} catch(Exception e) {
			jsonMap.put("statusCode", 500);
			jsonMap.put("msg", e.getMessage());
			BaseLog.e(this.getClass(), "addUserToRole将用户添加到角色中失败", e);
		}
		return jsonMap;
	}

	/**
	 * 删除角色关联的用户
	 *
	 * @return map 成功标志
	 * @description: 将与角色关联的用户关系数据删除
	 * @author:zhangli
	 * @date:2013-8-1
	 */
	@RequestMapping("delUserFromRole")
	@ResponseBody
	public Map<String, Object> delUserFromRole(String roleid, String userids) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			sysRole.delUserFromRole(userids, roleid);
			jsonMap.put("statusCode", 200);
		} catch(Exception e) {
			jsonMap.put("statusCode", 500);
			jsonMap.put("msg", e.getMessage());
			BaseLog.e(this.getClass(), "delUserFromRole从角色中删除用户失败", e);
		}
		return jsonMap;
	}
}
